// 手写代码实现个react~hook，reactSafeState，用于组件卸载后，异步请求返回数据设置state报错的解决
// 手写react hooks还不会，下面用react hook简单实现 reactSafeState
import { useState, useEffect } from 'react';

const useReactSafeState = (data) => {
  let isUpload = false;
  const [safeState, setSafeState] = useState(data);

  useEffect(() => {
    return () => {
      isUpload = true;
    };
  }, []);

  const customSafeData = (newData) => {
    if (!isUpload) {
      setSafeState(newData);
    }
  };

  return [safeState, customSafeData];
};

export default useReactSafeState;
